<?php
/**
 * Zead
 *
 * LICENSE	
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/bsd-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to eu@marcelomx.com so we can send you a copy immediately.
 *
 * @category   Zead
 * @author     Marcelo Rodrigues Gonzaga <eu@marcelomx.com>
 * @copyright  Copyright (c) 2008 Marcelo Rodrigues Gonzaga
 * @link       http://code.google.com/p/zead 
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License
 *
 **/

/**
 * @category   Zead
 * @package    Zead_PagesRange
 * @copyright  Copyright (c) 2008 Marcelo Rodrigues Gonzaga 
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License  
 */
class Zead_PagesRange extends ArrayIterator
{
	/**
	 * Adds a new page to range
	 * 
	 * @param string $url   The page url
	 * @param string $label The page label
	 * @param string $title The page title
	 */
	public function append($url, $label = null, $title = null, $position = 'end') 
	{
		$page = new stdClass();
		$page->url   = (string) $url;
		$page->label = (string) $label;
		$page->title = (string) $title;
		
		parent::append($page);
		
		return $this;
	}
	
	public function prepend($url, $label = null, $title = null)
	{
		$page = new stdClass();
		$page->url   = (string) $url;
		$page->label = (string) $label;
		$page->title = (string) $title;
		
		array_unshift($this, $page);
		return $this;
	}
	
	/**
	 * Return the current object page
	 * @return stdClass
	 */
	public function getCurrentPage() 
	{
		return $this->current();
	}
	
	/**
	 * Returns the current page url
	 * @return string
	 */
	public function getCurrentPageUrl() 
	{
		return $this->getCurrentPage()->url;
	}
	
	/**
	 * Returns the current page label
	 * @return string
	 */
	public function getCurrentPageLabel() 
	{
		return $this->getCurrentPage()->label;
	}
	
	/**
	 * Returns the current page title
	 * @return string
	 */
	public function getCurrentPageTitle() 
	{
		return $this->getCurrentPage()->title;
	}
	
	/**
	 * Return page number from a url
	 *
	 * @param  string $url
	 * @return int
	 */
	public function getPageNumber($url = null)
	{
		$offset = 1;
		
		if (null !== $url) {
			foreach ($this as $index => $page) {
				if ($url === $page->url) {
					$offset = $index + 1;
					break;
				}
			}
		}
		return $offset;
	}	
	
	/**
	 * Find and returns the current page from a url
	 * @param  string $url
	 * @return int
	 */
	public function getCurrentPageNumber()
	{
		return $this->key() + 1;
	}
	
	/**
	 * Returns a page from a number
	 * @param int $pageNumber
	 */
	public function getPage($pageNumber) 
	{
		$pageNumber = $pageNumber - 1;
		
		if ($pageNumber <= $this->count()) {
			return $this->offsetGet($pageNumber);
		}
	}
}
